<?php

// application/models/TblcompanyMapper.php
class Application_Model_TblcompanyMapper {

    protected $_dbTable;
    private $logger;

    public function init() {
        $this->logger = Zend_Registry::get('logger');
        $this->logger->info("Hello from Application_Model_TblcompanyMapper");
    }

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Tblcompany');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Tblcompany $tblcompany) {
        $data = array(
            'id' => $tblcompany->getId(),
            'siret' => $tblcompany->getSiret(),
            'companycode' => $tblcompany->getCompanycode(),
            'nom' => $tblcompany->getNom(),
            'adresse01' => $tblcompany->getAdresse01(),
            'codepostal01' => $tblcompany->getCodepostal01(),
            'pays01' => $tblcompany->getPays01(),
            'tel01' => $tblcompany->getTel01(),
            'fax01' => $tblcompany->getFax01(),
            'email01' => $tblcompany->getEmail01(),
            'adresse02' => $tblcompany->getAdresse02(),
            'codepostal02' => $tblcompany->getCodepostal02(),
            'pays02' => $tblcompany->getPays02(),
            'tel02' => $tblcompany->getTel02(),
            'fax02' => $tblcompany->getFax02(),
            'email02' => $tblcompany->getEmail02(),
            'description' => $tblcompany->getDescription(),
            'active' => $tblcompany->getActive(),
            'fournisseur' => $tblcompany->getFournisseur(),
            'client' => $tblcompany->getClient(),
            'level' => $tblcompany->getLevel(),
            'last_updated_on' => date('Y-m-d H:i:s')
        );

        if (null === ($id = $tblcompany->getId())) {
            Zend_Debug::dump($tblcompany->getId(), $label = null, $echo = true);
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }

        $this->logger = Zend_Registry::get('logger');
        $this->logger->info(Zend_Debug::dump($data, $label = null, $echo = true));
    }


    
    public function deleteById($delete_id) {
        $where = $this->getDbTable()->getAdapter()->quoteInto('id = ?', (int) $delete_id);
        $this->getDbTable()->delete($where);
    }

    public function find($id, Application_Model_Tblcompany $tblcompany) {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $tblcompany->setId($row->id)
                ->setSiret($row->siret)
                ->setCompanycode($row->companycode)
                ->setNom($row->nom)
                ->setDescription($row->description)
                ->setAdresse01($row->adresse01)
                ->setCodepostal01($row->codepostal01)
                ->setPays01($row->pays01)
                ->setEmail01($row->email01)
                ->setFax01($row->fax01)
                ->setTel01($row->tel01)
                ->setAdresse02($row->adresse02)
                ->setCodepostal02($row->codepostal02)
                ->setPays02($row->pays02)
                ->setEmail02($row->email02)
                ->setFax02($row->fax02)
                ->setTel02($row->tel02)
                ->setActive($row->active)
                ->setFournisseur($row->fournisseur)
                ->setClient($row->client)
                ->setLevel($row->level)
                ->setLast_updated_on($row->last_updated_on)
        ;
    }

    public function fetchAll() {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Tblcompany();
            $entry->setId($row->id)
                    ->setSiret($row->siret)
                    ->setCompanycode($row->companycode)
                    ->setNom($row->nom)
                    ->setDescription($row->description)
                    ->setAdresse01($row->adresse01)
                    ->setCodepostal01($row->codepostal01)
                    ->setPays01($row->pays01)
                    ->setEmail01($row->email01)
                    ->setFax01($row->fax01)
                    ->setTel01($row->tel01)
                    ->setAdresse02($row->adresse02)
                    ->setCodepostal02($row->codepostal02)
                    ->setPays02($row->pays02)
                    ->setEmail02($row->email02)
                    ->setFax02($row->fax02)
                    ->setTel02($row->tel02)
                    ->setActive($row->active)
                    ->setFournisseur($row->fournisseur)
                    ->setClient($row->client)
                    ->setLevel($row->level)
                    ->setLast_updated_on($row->last_updated_on)
            ;
            $entries[] = $entry;
        }
        return $entries;
    }
}
